﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlbumArtOrganizer.Plugins.WindowsMediaPlayer.Model
{
    public class CoverImage : AlbumArtOrganizer.Model.CoverImage, AlbumArtOrganizer.Model.ICoverImage
    {
        string _path = null;
        internal string Path
        {
            get
            {
                return _path;
            }
            set
            {
                _path = value;
                PublishPathChangedNotfication();
            }
        }

        bool? _exists = null;
        public override bool Exists
        {
            get
            {
                if (!_exists.HasValue)
                {
                    if (String.IsNullOrEmpty(Path))
                    {
                        _exists = false;
                    }
                    else
                    {
                        _exists = System.IO.File.Exists(Path);
                    }
                }
                return _exists.Value;
            }
        }

        public override System.Windows.Media.Imaging.BitmapImage Image
        {
            get
            {
                var image = new System.Windows.Media.Imaging.BitmapImage();
                image.BeginInit();
                image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                image.CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.DelayCreation | System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat;
                image.UriSource = new Uri(Path, UriKind.RelativeOrAbsolute);
                image.EndInit();
                return image;
            }
        }

        int? _pixelWidth = null;
        public override int PixelWidth
        {
            get
            {
                if (!_pixelWidth.HasValue && Exists)
                {
                    PopulateImageDimensions();
                }
                if (_pixelWidth.HasValue)
                {
                    return _pixelWidth.Value;
                }
                return -1;
            }
        }

        int? _pixelHeight = null;
        public override int PixelHeight
        {
            get
            {
                if (!_pixelHeight.HasValue && Exists)
                {
                    PopulateImageDimensions();
                }
                if (_pixelHeight.HasValue)
                {
                    return _pixelHeight.Value;
                }
                return -1;
            }
        }

        public override System.Windows.Media.Imaging.BitmapImage ThumbnailImage
        {
            get
            {
                var thumbnailImage = new System.Windows.Media.Imaging.BitmapImage();
                thumbnailImage.BeginInit();
                thumbnailImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                thumbnailImage.CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.DelayCreation;
                thumbnailImage.DecodePixelWidth = 100;
                thumbnailImage.UriSource = new Uri(Path, UriKind.RelativeOrAbsolute);
                thumbnailImage.EndInit();
                thumbnailImage.Freeze();
                return thumbnailImage;
            }
        }

        internal void PublishPathChangedNotfication()
        {
            _exists = null;
            PopulateImageDimensions();
            OnPropertyChanged("Exists");
            OnPropertyChanged("Path");
            OnPropertyChanged("PixelHeight");
            OnPropertyChanged("PixelWidth");
            OnPropertyChanged("Image");
            OnPropertyChanged("ThumbnailImage");
        }

        Tuple<int, int> GetImageDimensions()
        {
            if (Exists)
            {
                var tempImage = new System.Windows.Media.Imaging.BitmapImage();
                tempImage.BeginInit();
                tempImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                tempImage.CreateOptions = System.Windows.Media.Imaging.BitmapCreateOptions.DelayCreation | System.Windows.Media.Imaging.BitmapCreateOptions.IgnoreImageCache;
                tempImage.UriSource = new Uri(Path, UriKind.RelativeOrAbsolute);
                tempImage.EndInit();
                return Tuple.Create(tempImage.PixelWidth, tempImage.PixelHeight);
            }
            return null;
        }

        void PopulateImageDimensions()
        {
            var dimensions = GetImageDimensions();
            if (dimensions != null)
            {
                _pixelWidth = dimensions.Item1;
                _pixelHeight = dimensions.Item2;
            }
        }
    }
}
